from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import BlogDriver

#测试博客编辑页面

class BlogEdit:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_edit.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    #页面元素检查
    def InterfaceElement(self):
        self.driver.find_element(By.CSS_SELECTOR, "#title")
        self.driver.find_element(By.CSS_SELECTOR, "#submit")
        self.driver.find_element(By.CSS_SELECTOR, "#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre")

        # 测试主页、写博客、注销按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(4)")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(5)")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)")

    #登陆状态下
    def EditByLogin(self):
        #正常写博客
        self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("开开心心")
        self.driver.find_element(By.CSS_SELECTOR, "#editor > div.editormd-toolbar > div > ul > li:nth-child(21) > a > i").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > a")

        self.driver.back()
        #异常写博客
        self.driver.implicitly_wait(2)
        self.driver.find_element(By.CSS_SELECTOR, "#title").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        #等待弹窗
        wait = WebDriverWait(self.driver, 2)
        wait.until(EC.alert_is_present())
        alert = self.driver.switch_to.alert

        alert.accept()





